package com.sichen.product.service.impl;

import com.sichen.common.utils.R;
import com.sichen.product.vo.AttrGroupRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sichen.common.utils.PageUtils;
import com.sichen.common.utils.Query;

import com.sichen.product.dao.AttrAttrgroupRelationDao;
import com.sichen.product.entity.AttrAttrgroupRelationEntity;
import com.sichen.product.service.AttrAttrgroupRelationService;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    /**
     * 查询属性和属性分组关联列表,并进行分页展示
     * @param params 前端传递的分页参数
     * @return 查询的属性和属性分组列表 , 并且分页过的
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<>());
        return new PageUtils(page);
    }


    /**
     * 添加属性与分组关联关系
     * @param vos 属性与属性分组的关联关系的集合
     * @return 新增关系是否成功
     */
    @Override
    public R saveBatch(List<AttrGroupRelationVo> vos) {
        //1.收集所有的关联关系
        List<AttrAttrgroupRelationEntity> collect = vos.stream().map(item -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());
        //2.批量保存
        this.saveBatch(collect);
        return R.ok();
    }

}